package com.tz.springmvc.sysmanage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tz.springmvc.sysmanage.entity.User;
import com.tz.springmvc.sysmanage.service.IUserService;
 
/**
 * 用注解的方式开发用户的增删改查功能
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/sysmanage/user")
public class UserController{
 	@Autowired
	private IUserService userService;
	 
	@RequestMapping("/getUserListAjaxJson")
	public @ResponseBody List<User> getUserListAjaxJson(){
		List<User> userList = userService.getUserList(new User());
		return userList;
	}
	
	//进入用户管理页面
	@RequestMapping("/gotoUser")
	public String gotoUser(){
		return "sysmanage/user/userMain";		
	}
	
	//进入用户新增或者修改页面
	@RequestMapping("/gotoUserEdit")
	public String gotoUserEdit(Long userId,Model model){	
		if(userId!=null){//进入的是修改页面
			User user = new User();
			user = userService.getUserById(userId);
			model.addAttribute("user", user);
		}
		return "sysmanage/user/userEdit";		
	}
	
	@RequestMapping("/getUserByIdAjaxJson")
	public @ResponseBody User getUserByIdAjaxJson(Long userId){
		User user = new User();
		user = userService.getUserById(userId);
		return user;
	}
	
	 
	@RequestMapping("/userSaveAjaxJson")
	public @ResponseBody Map<String,Object> userSaveAjaxJson(@RequestBody @Validated User user,
							BindingResult bindingResult){		
		 Map<String,Object> resultMap = new HashMap<String,Object>();
		 
		 
		//如果校验时有不符合校验规则情况的出现,他会讲校验的提示信息放在bindingResult的 errors对象里面
		if(bindingResult.hasErrors()){
			List<ObjectError> errors =bindingResult.getAllErrors();
			resultMap.put("resultValidator", true);
			String errorMessage = "";
 			for(ObjectError error:errors){
 				errorMessage = errorMessage+error.getDefaultMessage()+"\n";
 				System.out.println(error.getDefaultMessage());
			}
			resultMap.put("errorMessage", errorMessage);

		}else{		 
			 if(user.getUserId()!=null){
				if(userService.updateUser(user)){
					resultMap.put("result", "修改用户成功") ; 
				}else{
					resultMap.put("result", "修改用户失败");  
				}	
			 }else{
				 if(userService.addUser(user)){
						resultMap.put("result", "新增成功") ; 
					}else{
						resultMap.put("result", "新增用户失败");  
					} 
			 }
		}	 
		return resultMap;
	}
	
	
	
	 
	@RequestMapping("/userDelAjaxJson")
	public @ResponseBody Map<String,Object> userDelAjaxJson(Long userId){
		 Map<String,Object> resultMap = new HashMap<String,Object>();
		if(userId!=null){
			if(userService.delUser(userId)){
				resultMap.put("result", "删除用户成功") ; 
			}else{
				resultMap.put("result", "删除用户失败");  
			}
		}		 
		return resultMap;
	}
  
	 
	
	
}
